//
//  CZStatusCell.swift
//  crkWeibo
//
//  Created by 陈荣科 on 15/12/23.
//  Copyright © 2015年 crk. All rights reserved.
//

import UIKit

//微博Cell
class CZStatusCell: UITableViewCell {
    
    //cell显示内容需要模型
    var status: CZStatus? {
    
        didSet {
            //给topView传递模型数据
            topView.status = status
            
            //给微博内容赋值
            contentLabel.text = status?.text
            
            
            //将模型设置给图片视图
            pictureView.status = status
            
            //调用视图的方法返回CGSize
            let size = pictureView.calcViewSize()
//            print("size:\(size)")
            //给图片的宽高赋值
            pictureViewHeightCon?.constant = size.height
            pictureViewWidthCon?.constant = size.width
        }
    }
    
    ///配图的宽度约束
    var pictureViewWidthCon: NSLayoutConstraint?
    
    ///配图的高度约束
    var pictureViewHeightCon: NSLayoutConstraint?
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style:style , reuseIdentifier: reuseIdentifier)
        
        //准备UI
        prepareUI()
        
    }
    
    ///MARK: - 计算cell的行高
    ///计算cell的行高
    func rowHeight(status: CZStatus) ->CGFloat{
        
        self.status = status
        //刷新布局
        layoutIfNeeded()
        
        //获取底部视图的最大y值
      let maxY = CGRectGetMaxY(bottomView.frame)
        return maxY
    }

    func prepareUI() {
    
        //1.添加子控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        //2.添加约束
        topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        
        //微博文本内容赋值
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        
        //微博文本内容的宽度
         contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2*8))
//        
//       let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
//        
//        pictureViewHeightCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        
//        pictureViewWidthCon = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
        
        
        //底部视图的约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
        
        //要想让cell的高度适应内容高度，就要让contenLabel的底部和contenView的底部重合
        //TODO:有约束冲突
        
//        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bottomView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    
    }
    
    //MARK: - 懒加载
    
    /// 顶部视图
    private lazy var topView:CZTopView = CZTopView()
    
    
    ///中间图片占位
    lazy var pictureView:CZStatusPictureView = CZStatusPictureView()
    
    ////底部视图
     lazy var bottomView:CZStatusBottomView = CZStatusBottomView()
    
    ///微博文本内容
     lazy var contentLabel:UILabel = {
    
        let label = UILabel()
        
        //字体大小
        label.font = UIFont.systemFontOfSize(14)
        //自动换行
        label.numberOfLines = 0
        //颜色
        label.textColor = UIColor.darkGrayColor()
        
        return label
    }()
}
